
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import antlr.CommonAST;
import antlr.debug.misc.ASTFrame;

/**
 * 
 * @author shilpa
 * 
 */
public class TwinkleMain {

	public static void main(String[] args) throws Exception {
		String inputFile = args.length > 0 ? args[0] : null;
		FileInputStream fileInput = null;

		try {
			File path = new File("");
			// inputFile =new
			// String(path.getAbsolutePath()+"\\src\\"+"AnimalShowIfPressed.t");
			// inputFile =new
			// String(path.getAbsolutePath()+"\\src\\"+"HelloWorld.twkl");

			// inputFile =new
			// String(path.getAbsolutePath()+"\\src\\"+"AnimalShowIfPressed.t");
			// inputFile =new
			// String(path.getAbsolutePath()+"\\src\\"+"showAllNumbersTest.t");
			// inputFile =new
			// String(path.getAbsolutePath()+"\\src\\twinkleTest\\"+"letsCountShowAndTest.t");

			if(inputFile == null) {
			inputFile = new String(path.getAbsolutePath()
					+ "\\src\\twinkleTest\\" + "LearnToRead.t");
			}
			if (!inputFile.endsWith(".t")) {
				System.out.println("Invalid input file " + inputFile
						+ ". Twikle files should have .t extension.");
				System.exit(0);
			}
			// inputFile =new
			// String(path.getAbsolutePath()+"\\src\\twinkleTest\\"+"showMessageTest.t");
			System.out.println("file " + inputFile);
			fileInput = new FileInputStream(inputFile);
		} catch (Exception e) {
			error();
		}

		try {

			DataInputStream input = new DataInputStream(fileInput);
			TwinkleLexer lexer = new TwinkleLexer(input);
			TwinkleParser parser = new TwinkleParser(lexer);
			parser.program();
			CommonAST tree = (CommonAST) parser.getAST();
			System.out.println("=============AST Structure=============");
			System.out.println(tree.toStringList());
			System.out.println("=============     END     =============");
			ASTFrame frame = new ASTFrame("AST JTree Example", tree);
			frame.setVisible(true);

			TwinkleTreeParser treeParser = new TwinkleTreeParser();
			treeParser.program(tree);

		} catch (Exception e) {
			System.err.println(e.getMessage());
			System.exit(0);
		}
	}

	private static void error() {

		System.out.println("*-----------------------------*");
		System.out.println("| USAGE:                      |");
		System.out.println("|   java TwinkleMain inputfile |");
		System.out.println("*-----------------------------*");
		System.exit(0);
	}
}
